package com.tma.umedia;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class HomeActivity extends Activity {
		
		ListView list;
	    LazyAdapter adapter;
	    public ArrayList<Song> playlist = new ArrayList<Song>();
	    float historicX = Float.NaN, historicY = Float.NaN;
	    static final int TRIGGER_DELTA = 50; // Number of pixels to travel till trigger
	    
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_home);
			list=(ListView)findViewById(R.id.list);
			
			Utilities utils = new Utilities();
			playlist = this.getIntent().getParcelableArrayListExtra("playlist");
			if(playlist == null || playlist.isEmpty())
				playlist = utils.getPlayList(Environment.getExternalStorageDirectory().getPath());
			
			// Getting adapter by passing xml data ArrayList
	        adapter = new LazyAdapter(this, playlist);
	        list.setAdapter(adapter);
	        
	        // registerForContextMenu(list);
	        
	        // Click event for single list row
	        list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// Starting new intent
					Intent in = new Intent(getApplicationContext(),	PlayerActivity.class);
					
					// Sending songIndex to PlayerActivity
					ArrayList<Song> sendlist = new ArrayList<Song>();
					for(int i=0; i < playlist.size(); i++){
						sendlist.add(playlist.get(i));
					}
					in.putParcelableArrayListExtra("playlist", sendlist);
					in.putExtra("index", position);
					
					// starting new activity
					startActivityForResult(in, 100);
				}
			});		
		}	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.favourite:    
            	Toast.makeText(this, "You pressed favourite!", Toast.LENGTH_LONG).show();
                break;
            case R.id.open:
            	// Starting new intent
				Intent in = new Intent(getApplicationContext(),	FolderActivity.class);
				
				// Sending songIndex to PlayerActivity
				ArrayList<Song> sendlist = new ArrayList<Song>();
				for(int i=0; i < playlist.size(); i++){
					sendlist.add(playlist.get(i));
				}
				in.putParcelableArrayListExtra("playlist", sendlist);
				
				// starting new activity
				startActivityForResult(in, 100);
                break;
        }
        return true;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
}
